import requests
import re

cookies = {
    'NMTID': '00OFSWbCgOMluMGuEVfrQc-O0-1ozsAAAGT058e8w',
    '_iuqxldmzr_': '32',
    '_ntes_nnid': 'ffaeb9964079d1849902e5ab1785ccc0,1734422241860',
    '_ntes_nuid': 'ffaeb9964079d1849902e5ab1785ccc0',
    'WEVNSM': '1.0.0',
    'WNMCID': 'cvecrb.1734422242289.01.0',
    'WM_TID': 'JGMdvgKjTMJFBAUEFBKXW7dLwvtIziZM',
    'sDeviceId': 'YD-8a%2FOZ4a2eLpBQ0VAVUbEfDaAmroXiwiC',
    'ntes_utid': 'tid._.B%252Bj0mftTmtBFUwUFVRLWG%252BJal7sPVLzA._.0',
    '__snaker__id': 'Z8uYJfWDtpE2CJDT',
    '__remember_me': 'true',
    'MUSIC_U': '00A51C7CF37C3972FA3EAF74E27764A88DC3FD9824F90A6D610515C6C72B296CBBB88E9DE8A2714CEF8430930E3C3968A759188DEFB72404EA8AD2F6E7A4F3539D62B99E7971B6951031A36F019A59B3422A4B129B1A66F7D81B0FB9F7126C7D3E50DFF379902F6266579A3797C7BBE4A34B425F87FCE24C5A8E42E590DD1EB4FBE4E675E6720A180AD0F228A6DBD71D77F65F5F214122F25A70AAFDD6B2F9CF9C7D9D2828CAF3DB21612BE52D84F3E25E5E16DF41DC79ADD90E22C8908399B79DB978E9956A20864C4DBBA92D82540EE41EBCEB1EDD453662778BE74681F42C99B588119E6090A666CF69BD72D607BD5C11F98499178B8CD3187CA6A1A0B00B3AB79A385E9FBEA75A417A5968894C62591C4B5C8E9E07942720B1F33706C68E66747B2D18FAA17E9EAC2F22C67BC160B50BBAB0E9CFAA8C3AF5A309B3C6EB82F9B1D88D88024669519C7D024156BAB27203A7300C37C0924CB6EB7B785249C711',
    '__csrf': 'ff223204ff2f3b3e9ffaa6ea4fccee45',
    'ntes_kaola_ad': '1',
    'gdxidpyhxdE': 'nCtwLu5aw19f8ZRG4VUvRp9HBIx7HqATbIWh9H00a%5C4jC2CYShg%5CkhqtsBM9%2BlhsT9KMzpU96P92Dcxu%2FAgjSXMM%2FIdWsed%2FYZnLk%5CaYnMay%2F6xP9K5tqYf8MqnAXLwIk38MhGA61nxWVNjkNl0CJ%5CySQU1krSXO18obgp9Ce7pBLVHH%3A1734424931193',
    'JSESSIONID-WYYY': 'Ynk4ymdaqdzh0awnr%5CDPwm535CnXJv0UPCHYkubnTtZeeX5h%2BT6nG4l0c9yJbJhfPyD1YS551VeloB%2FNNJEYTnseJi%2FtQCvQEuV146l%2BfP7ABsFx%2BO0bB0qtOHjzdFNqrFh9K0%2BU8ytYM%2F80%2B%2FaZ9KSYuqnK4kak7jx9XWkuqVf1KsOI%3A1734570972136',
    'WM_NI': 'f5aHIhQUGQLH6ozX6yLmxIJbNNJLpo7jOZa0X4h7Wiecp0mx1HMD2ySUnD7zR35lWVMVookL%2FF4XThckRi1lNyUQMwi%2BNQnvevVDyNw8XuLJtwst06EBF10EKJPzKTjgY0Q%3D',
    'WM_NIKE': '9ca17ae2e6ffcda170e2e6ee9af45dacea8dafc47db2e78ba6d45e938f9eb1cb59f690a8adc14298eafed9cc2af0fea7c3b92aa6ebafa9cc64938b89b2cf44f6b1b6b9b57390b3b8aac96a8e9286adf365f5b681bbe43c9ceca8d8d663a7bdc0d1f074f39883a5b173918ba2a5b67292edfb8dd65e8f8aa190ea48ed9afcb9d764a1b59e8ab4688ea79bb2d07dabf582b2ed7ea7aa9aa5fc46b5e89896cf42b8a9bd95b33e879e8ad2b34f8996a09bca60aab997d1d437e2a3',
}

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'zh-CN,zh;q=0.9',
    # 'cookie': 'NMTID=00OFSWbCgOMluMGuEVfrQc-O0-1ozsAAAGT058e8w; _iuqxldmzr_=32; _ntes_nnid=ffaeb9964079d1849902e5ab1785ccc0,1734422241860; _ntes_nuid=ffaeb9964079d1849902e5ab1785ccc0; WEVNSM=1.0.0; WNMCID=cvecrb.1734422242289.01.0; WM_TID=JGMdvgKjTMJFBAUEFBKXW7dLwvtIziZM; sDeviceId=YD-8a%2FOZ4a2eLpBQ0VAVUbEfDaAmroXiwiC; ntes_utid=tid._.B%252Bj0mftTmtBFUwUFVRLWG%252BJal7sPVLzA._.0; __snaker__id=Z8uYJfWDtpE2CJDT; __remember_me=true; MUSIC_U=00A51C7CF37C3972FA3EAF74E27764A88DC3FD9824F90A6D610515C6C72B296CBBB88E9DE8A2714CEF8430930E3C3968A759188DEFB72404EA8AD2F6E7A4F3539D62B99E7971B6951031A36F019A59B3422A4B129B1A66F7D81B0FB9F7126C7D3E50DFF379902F6266579A3797C7BBE4A34B425F87FCE24C5A8E42E590DD1EB4FBE4E675E6720A180AD0F228A6DBD71D77F65F5F214122F25A70AAFDD6B2F9CF9C7D9D2828CAF3DB21612BE52D84F3E25E5E16DF41DC79ADD90E22C8908399B79DB978E9956A20864C4DBBA92D82540EE41EBCEB1EDD453662778BE74681F42C99B588119E6090A666CF69BD72D607BD5C11F98499178B8CD3187CA6A1A0B00B3AB79A385E9FBEA75A417A5968894C62591C4B5C8E9E07942720B1F33706C68E66747B2D18FAA17E9EAC2F22C67BC160B50BBAB0E9CFAA8C3AF5A309B3C6EB82F9B1D88D88024669519C7D024156BAB27203A7300C37C0924CB6EB7B785249C711; __csrf=ff223204ff2f3b3e9ffaa6ea4fccee45; ntes_kaola_ad=1; gdxidpyhxdE=nCtwLu5aw19f8ZRG4VUvRp9HBIx7HqATbIWh9H00a%5C4jC2CYShg%5CkhqtsBM9%2BlhsT9KMzpU96P92Dcxu%2FAgjSXMM%2FIdWsed%2FYZnLk%5CaYnMay%2F6xP9K5tqYf8MqnAXLwIk38MhGA61nxWVNjkNl0CJ%5CySQU1krSXO18obgp9Ce7pBLVHH%3A1734424931193; JSESSIONID-WYYY=Ynk4ymdaqdzh0awnr%5CDPwm535CnXJv0UPCHYkubnTtZeeX5h%2BT6nG4l0c9yJbJhfPyD1YS551VeloB%2FNNJEYTnseJi%2FtQCvQEuV146l%2BfP7ABsFx%2BO0bB0qtOHjzdFNqrFh9K0%2BU8ytYM%2F80%2B%2FaZ9KSYuqnK4kak7jx9XWkuqVf1KsOI%3A1734570972136; WM_NI=f5aHIhQUGQLH6ozX6yLmxIJbNNJLpo7jOZa0X4h7Wiecp0mx1HMD2ySUnD7zR35lWVMVookL%2FF4XThckRi1lNyUQMwi%2BNQnvevVDyNw8XuLJtwst06EBF10EKJPzKTjgY0Q%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee9af45dacea8dafc47db2e78ba6d45e938f9eb1cb59f690a8adc14298eafed9cc2af0fea7c3b92aa6ebafa9cc64938b89b2cf44f6b1b6b9b57390b3b8aac96a8e9286adf365f5b681bbe43c9ceca8d8d663a7bdc0d1f074f39883a5b173918ba2a5b67292edfb8dd65e8f8aa190ea48ed9afcb9d764a1b59e8ab4688ea79bb2d07dabf582b2ed7ea7aa9aa5fc46b5e89896cf42b8a9bd95b33e879e8ad2b34f8996a09bca60aab997d1d437e2a3',
    'priority': 'u=0, i',
    'referer': 'https://music.163.com/',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'iframe',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'same-origin',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}

params = {
    'id': '19723756',
}

#'https://music.163.com/song/media/outer/url?id='
if __name__ == "__main__":
    response = requests.get('https://music.163.com/discover/toplist', params=params, cookies=cookies, headers=headers)
    data = re.findall(r'<li><a href="/song\?id=(.*?)">(.*?)</a></li>',response.text,re.S)
    temp = 'https://music.163.com/song/media/outer/url?id='
    for i in data:
        url = temp + i[0]
        with open('music\\' + i[1] + '.mp3', 'wb') as f:
            f.write(requests.get(url).content)
        f.close()